/**
 * Copyright &copy; 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.register.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jeeplus.aop.demo.annotation.DemoMode;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.common.redis.RedisUtils;
import com.jeeplus.core.errors.ErrorConstants;
import com.jeeplus.register.service.dto.RegisterUserDTO;
import com.jeeplus.security.util.SecurityUtils;
import com.jeeplus.servicecenter.controller.AssociationApplyController;
import com.jeeplus.servicecenter.domain.ActivityStatistics;
import com.jeeplus.servicecenter.service.ActivityStatisticsService;
import com.jeeplus.servicecenter.service.dto.AssociationApplyDTO;
import com.jeeplus.sys.constant.CacheNames;
import com.jeeplus.sys.domain.Office;
import com.jeeplus.sys.domain.Post;
import com.jeeplus.sys.domain.Role;
import com.jeeplus.sys.service.OfficeService;
import com.jeeplus.sys.service.PostService;
import com.jeeplus.sys.service.RoleService;
import com.jeeplus.sys.service.UserService;
import com.jeeplus.sys.service.dto.OfficeDTO;
import com.jeeplus.sys.service.dto.PostDTO;
import com.jeeplus.sys.service.dto.RoleDTO;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户Controller
 *
 * @author jeeplus
 * @version 2021-8-29
 */
@Api(tags = "用户注册")
@RestController
@RequestMapping("/register/user")
public class RegisterController {


    @Autowired
    private UserService userService;

    @Autowired
    private ActivityStatisticsService activityStatisticsService;

    @Autowired
    private OfficeService officeService;

    @Autowired
    private PostService postService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AssociationApplyController associationApplyController;
    /**
     * 保存用户
     *
     * @param registerUserDTO
     * @return
     */
    @DemoMode
    @ApiLog("生成用户")
    @ApiOperation(value = "生成用户")
    @PostMapping("register")
    @Transactional
    public ResponseEntity save(@Valid @RequestBody RegisterUserDTO registerUserDTO) {
        String code = registerUserDTO.getCode ( );
        if ( !code.equals ( RedisUtils.getInstance ( ).get ( CacheNames.SYS_CACHE_CODE, registerUserDTO.getUuid ( ) ) ) ) {
            return ResponseEntity.badRequest ( ).body ( ErrorConstants.LOGIN_ERROR_ERROR_VALIDATE_CODE );
        }
        UserDTO userDTO = BeanUtil.copyProperties(registerUserDTO, UserDTO.class);
        userDTO.setPassword ( SecurityUtils.encryptPassword ( userDTO.getNewPassword ( ) ) );
        if ( !UserUtils.isCheckLoginName ( userDTO.getOldLoginName ( ), userDTO.getLoginName ( ) ) ) {
            return ResponseEntity.badRequest ( ).body ( "保存用户'" + userDTO.getLoginName ( ) + "'失败，登录名已存在!" );
        }
        // 保存用户信息
        Office officeServiceOne1 = officeService.getOne(new QueryWrapper<Office>().eq("name", "校外人员"));
        Office officeServiceOne2 = officeService.getOne(new QueryWrapper<Office>().eq("name", "无"));
        Post postServiceOne = postService.getOne(new QueryWrapper<Post>().eq("name", "无"));
        Role roleServiceOne = roleService.getOne(new QueryWrapper<Role>().eq("name", "非学校职工"));
        OfficeDTO companyDTO = BeanUtil.copyProperties(officeServiceOne1, OfficeDTO.class);
        OfficeDTO officeDTO = BeanUtil.copyProperties(officeServiceOne2, OfficeDTO.class);
        List<PostDTO> listPost=new ArrayList<>();
        List<RoleDTO> roleDTOList=new ArrayList<>();
        listPost.add(BeanUtil.copyProperties(postServiceOne, PostDTO.class));
        roleDTOList.add(BeanUtil.copyProperties(roleServiceOne, RoleDTO.class));
        userDTO.setCompanyDTO(companyDTO);
        userDTO.setOfficeDTO(officeDTO);
        userDTO.setPostDTOList(listPost);
        userDTO.setRoleDTOList(roleDTOList);
        userService.saveOrUpdate (userDTO);
        //初始化活跃度
        activityStatisticsService.saveOrUpdate(new ActivityStatistics(userDTO.getId(),0));
        //自动同意
        AssociationApplyDTO associationApplyDTO = new AssociationApplyDTO();
        associationApplyDTO.setId(userDTO.getId());
        associationApplyDTO.setLoginName(userDTO.getLoginName());
        associationApplyDTO.setCompanyId(officeServiceOne1.getId());
        associationApplyDTO.setOfficeId(officeServiceOne2.getId());
        associationApplyDTO.setPostId(postServiceOne.getId());
        associationApplyDTO.setRoleId(roleServiceOne.getId());
        associationApplyDTO.setName(userDTO.getName());
        associationApplyController.autoAgree(associationApplyDTO);
        return ResponseEntity.ok ( "保存用户'" + userDTO.getLoginName ( ) + "'成功!" );
    }
}
